package com.dinzeer.sjapadder.se;

import com.dinzeer.legendreliclib.lib.StackReg;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.register.SjaEffect;
import com.dinzeer.sjapadder.register.SjaSpecialEffectRegsitry;
import com.dinzeer.sjapadder.register.SjaStacksReg;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class ColdGuardian extends SpecialEffect {
    public ColdGuardian() {
        super(30);
    }

    public static Boolean isEffective(LivingEntity owner) {
        if (owner instanceof Player player) {
            if (!SpecialEffect.isEffective(SjaSpecialEffectRegsitry.ColdGuardian.getId(), player.experienceLevel)) {
                return false;
            }
        }
        if (!(owner.getMainHandItem().getItem() instanceof ItemSlashBlade)) {
            return false;
        }
        return SlashBladeUtil.hasSpecialEffect(owner.getMainHandItem(), SjaSpecialEffectRegsitry.ColdGuardian);
    }

    @SubscribeEvent
    public static void onLivingHurt(LivingHurtEvent event) {
        DamageSource source = event.getSource();
        Entity attacker = source.getEntity();



        if (event.getEntity().getMainHandItem().getItem() instanceof ItemSlashBlade&&event.getEntity() instanceof Player player){
            if (SlashBladeUtil.hasSpecialEffect(event.getEntity().getMainHandItem(), SjaSpecialEffectRegsitry.ColdGuardian)){
                float amount=event.getAmount();
                if (amount> SjaStacksReg.ColdGuardian.getCurrentStacks(player)){
                    amount-= SjaStacksReg.ColdGuardian.getCurrentStacks(player);
                    SjaStacksReg.ColdGuardian.addStacks(player, -SjaStacksReg.ColdGuardian.getCurrentStacks(player));
                }else if (amount<=SjaStacksReg.ColdGuardian.getCurrentStacks(player)){
                    SjaStacksReg.ColdGuardian.addStacks(player, (int) -amount);
                    amount=0;
                }

                event.setAmount(amount);
            }
        }






        // 检查攻击者是否有效
        if (!(attacker instanceof Player player) || !isEffective(player)) {
            return;
        }

        LivingEntity target = event.getEntity();

        // 获取ColderEclipse效果
        MobEffectInstance effect = target.getEffect(SjaEffect.ColdEclipse.get());
        int currentLevel = (effect != null) ? effect.getAmplifier() + 1 : 0;
        int newLevel = Math.min(currentLevel + 1, 5);

        // 应用新层数
        target.addEffect(new MobEffectInstance(
                SjaEffect.ColdEclipse.get(),
                200, // 10秒持续时间（20 ticks/秒）
                newLevel - 1, // 放大器从0开始
                false,
                true,
                true
        ));

        // 达到5层时触发爆炸
        if (newLevel >= 5) {
            // 清除效果
            target.removeEffect(SjaEffect.ColdEclipse.get());

            // 获取目标位置
            double x = target.getX();
            double y = target.getY();
            double z = target.getZ();

            // 创建3格半径的AABB
            AABB explosionArea = new AABB(
                    x - 3.0, y - 3.0, z - 3.0,
                    x + 3.0, y + 3.0, z + 3.0
            );

            // 对范围内所有生物造成伤害
            for (LivingEntity entity : target.level().getEntitiesOfClass(
                    LivingEntity.class, explosionArea, e -> e != player)) {
                entity.hurt(target.damageSources().magic(), 50.0f);
            }
        }
    }
    @SubscribeEvent
    public static void onKilled(LivingDropsEvent event){
        if (event.getSource().getEntity() instanceof Player player&&player.getMainHandItem().getItem() instanceof ItemSlashBlade){
            if(SlashBladeUtil.hasSpecialEffect(player.getMainHandItem(), SjaSpecialEffectRegsitry.ColdGuardian)){
                SjaStacksReg.ColdGuardian.addStacks( player,20);
            }
        }
    }
}